package cn.zjutleo.village_server.controller;

import cn.zjutleo.village_server.entity.RepairApply;
import cn.zjutleo.village_server.service.RepairApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author : Fu Lantian
 * @date : Created in 2021/11/17
 * @Description: 该类用于 查询所有的报修信息
 */
@Api(tags = "报修信息查询")
@RestController()
public class RepairInfoSearchController {
    @Autowired
    RepairApplyService repairApplyService;

    @ApiOperation("提交报修数据申请")
    @PostMapping("/api/submitRepairInfo")
    public int submitRepairInfo(RepairApply repairApply){
        return repairApplyService.submitRepairInfo(repairApply);
    }

    @ApiOperation("查询所有的报修信息")
    @PostMapping("/api/searchAllRepairInfo")
    public List<RepairApply> getAllInfo(){
        List<RepairApply> result= repairApplyService.selectAll();
        return result;
    }

    @ApiOperation("通过报修单号id查询报修信息")
    @PostMapping("/api/searchById")
    public List<RepairApply> getById(@RequestBody HashMap<String, String> map){
        List<RepairApply> result = new ArrayList<>();
        result.add(repairApplyService.selectById(Integer.valueOf(map.get("id"))));
        return result;
    }

    @ApiOperation("拉取报修类型")
    @PostMapping("/api/getAllRepairType")
    public List<String> getAllRepairType(){
        List<String> result = repairApplyService.getAllRepairType();
        return result;
    }

    @ApiOperation("通过时间,报修类型和模糊搜素问题描述查询")
    @PostMapping("/api/getRepairInfoFuzzySearch")
    public List<RepairApply> getRepairInfoFuzzySearch(@RequestBody HashMap<String, String> map){
        String date=map.get("date");
        date=(date.equals(""))?null:date;
        String type=map.get("type");
        type=(type.equals(""))?null:type;
        String fuzzySearch=map.get("fuzzySearch");
        fuzzySearch=(fuzzySearch.equals(""))?null:fuzzySearch;
        List<RepairApply> result = repairApplyService.getRepairInfoFuzzySearch(date,type,fuzzySearch);
        return result;
    }
    @ApiOperation("根据报修单号把报修单打回")
    @PostMapping("/api/dispatchReturn")
    public String dispatchReturn(@RequestBody HashMap<String ,String > map){
        String u_id=map.get("u_id");
        String ra_id=map.get("ra_id");
        Integer u_role_id= repairApplyService.getRoleRightById(Integer.valueOf(u_id));
        Integer result= repairApplyService.updateRaState(Integer.parseInt(ra_id),-1);
        return result==1?"成功更新":"更新失败";
    }
}
